//
//  MyCoinListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/5/30.
//

#import "MyCoinListCell.h"

@interface MyCoinListCell()

@property(nonatomic,strong)UIView *bgView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UIButton *finishBtn;

@end

@implementation MyCoinListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.backgroundColor = [UIColor clearColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    __weak typeof(self)self_weak_ = self;
    self.bgView = [[UIView alloc]init];
    self.bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bgView];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.top.bottom.mas_equalTo(@0);
    }];
    UIImageView *logoImageView = [[UIImageView alloc]init];
    logoImageView.image = [UIImage imageNamed:@"mine_point_list_coin"];
    [self.bgView addSubview:logoImageView];
    [logoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@28);
        make.left.mas_equalTo(@16);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    
    self.finishBtn = [[UIButton alloc]init];
    [self.finishBtn setTitle:@"去完成" forState:UIControlStateNormal];
    [self.finishBtn setTitle:@"已完成" forState:UIControlStateSelected];
    [self.finishBtn setTitleColor:KThemeColor forState:UIControlStateNormal];
    [self.finishBtn setTitleColor:UIColorHex(0xCCCCCC) forState:UIControlStateSelected];
    self.finishBtn.backgroundColor = UIColorHex(0xE8FAF5);
    self.finishBtn.layer.cornerRadius = 14;
    self.finishBtn.layer.masksToBounds = YES;
    self.finishBtn.titleLabel.font = JPUIFontPingFangSCRegular(14);
    [self.finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.bgView addSubview:self.finishBtn];
    [self.finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(@-16);
        make.height.mas_equalTo(@28);
        make.width.mas_equalTo(@66);
        make.centerY.mas_equalTo(self_weak_.bgView);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.numberOfLines = 2;
    [self.bgView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(logoImageView.mas_right).offset(12);
        make.right.mas_equalTo(self_weak_.finishBtn.mas_left).offset(-12);
        make.centerY.mas_equalTo(self_weak_.finishBtn);
    }];
    
    
}

-(NSMutableAttributedString*)changeStr:(NSString*)str andStr2:(NSString*)str2{
    NSMutableAttributedString *pStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  %@",str,str2]];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontBlackColor range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSForegroundColorAttributeName value:KFontGrayColor range:NSMakeRange(str.length,pStr.length-str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontPingFangSCMedium(15) range:NSMakeRange(0,str.length)];
    [pStr addAttribute:NSFontAttributeName value:JPUIFontPingFangSCMedium(14) range:NSMakeRange(str.length,pStr.length-str.length)];
    return pStr;
}

-(void)finishBtnClick{
    if(_toFinishBlock){
        _toFinishBlock();
    }
}

-(void)setModel:(TaskListModel *)model{
    _model = model;
    self.titleLabel.attributedText = [self changeStr:model.name andStr2:[NSString stringWithFormat:@"+%@岛币",model.gold_number]];
    if(model.finished.integerValue == 1){
        self.finishBtn.selected = YES;
        self.finishBtn.backgroundColor = KbgColor;
    }else{
        self.finishBtn.selected = NO;
        self.finishBtn.backgroundColor = UIColorHex(0xE8FAF5);
    }
}


@end
